const path = require("path")
const {
    override,
    addWebpackAlias,
    addWebpackPlugin,
} = require("customize-cra");
const { GenerateSW } = require('workbox-webpack-plugin')

const config = override(
    addWebpackAlias({
        '@': path.resolve(__dirname, './src'),
    }),
    addWebpackPlugin(new GenerateSW({
        cacheId: 'react-pwa', // 设置前缀
        cleanupOutdatedCaches: true, // 删除旧的预缓存
        skipWaiting: true,
        clientsClaim: true,
        mode: 'development', // 生成sw文件的模式
        swDest: 'service-worker.js', // 默认 service-worker.js
        exclude: ['service-wroker.js'], // 排除的文件
        runtimeCaching: [
            {
                urlPattern: /^http:\/\/juheimg.oss-cn-hangzhou.aliyuncs.com/,
                handler: 'NetworkFirst', // 网络优先
                options: {
                    cacheName: 'images-cache',
                    expiration: {
                        maxEntries: 20, // 针对改类型的缓存的最大数量，超过替换
                    },
                },
            },
            {
                urlPattern: /^http:\/\/127.0.0.1:3030\/api\/food\/getFoodList$/,
                handler: 'NetworkFirst', // 网络优先
                options: {
                    cacheName: 'api-cache',
                    expiration: {
                        maxAgeSeconds: 60 * 60 // 过期时间，/秒
                    },
                },
            }
        ]
    }))
);

module.exports = config;